package com.liuwang.meal.ui;

import java.io.IOException;
import java.net.UnknownHostException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.liuwang.meal.R;
import com.liuwang.meal.util.MyUtil;
import com.liuwang.meal.util.NetworkUtil;
import com.liuwang.meal.util.SocketUtil;

/**
 * 登陆页面
 * 
 * @author 陈江根
 * 
 */
public class LodingActivity extends Activity {

	// 声明控件
	private ProgressBar loading_progress_bar;
	BroadcastReceiverHelper rhelper;
	private SocketUtil client;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loading_anim);

		// 判断网络是否可用
		// CheckNetworkConnection();

		// 开启服务
//		startService(new Intent("com.liuwang.meal.Socket"));

		loading_progress_bar = (ProgressBar) findViewById(R.id.loading_progress_bar);
		// loading_progress_bar.setProgress(20);

		// ProgressDialog mypDialog = new ProgressDialog(this);
		// // 实例化
		// mypDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		// // 设置进度条风格，风格为长形，有刻度的
		// mypDialog.setTitle("同步数据");
		// // 设置ProgressDialog 标题
		// mypDialog.setMessage("正在同步服务器数据...");
		// // 设置ProgressDialog 提示信息
		// // mypDialog.setIcon(R.drawable.ic_launcher);
		// // 设置ProgressDialog 标题图标
		// mypDialog.setProgress(59);
		// // 设置ProgressDialog 进度条进度
		// // mypDialog.setButton("退出",
		// (android.content.DialogInterface.OnClickListener) this);
		// // 设置ProgressDialog 的一个Button
		// mypDialog.setIndeterminate(false);
		// // 设置ProgressDialog 的进度条是否不明确
		// mypDialog.setCancelable(true);
		// // 设置ProgressDialog 是否可以按退回按键取消
		// mypDialog.show();
		// // 让ProgressDialog显示
		
//		 //资源数据更新
//		try {
//			client = new SocketUtil("192.168.1.107",8898);
//		} catch (Exception e) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setMessage("服务器连接失败,请检查服务器是否开启...")
//			       .setCancelable(false)
//			       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//			           public void onClick(DialogInterface dialog, int id) {
//			        	   finish();
//			           }
//			       });
//			AlertDialog alert = builder.create();
//			alert.show();
//			return;
//		}
		
//		//TODO 服务器的当前时间
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				try {
//					Log.i("test", "返回的消息：" + client.sendMsg("0,synchronous,0,15,null"));
//				} catch (IOException e) {
//					MyUtil.showDialog(LodingActivity.this, "发送消息失败....");
//				}// /
//				client.closeSocket();
//			}
//		}).start();
			 
			 
			 
		

		// TODO 字段数据更新
		// try {
		// client = new SocketUtil("192.168.1.107",8898);
		// } catch (Exception e) {
		// Toast.makeText(LoginActivity.this, "服务器连接失败,请检查服务器是否开启...",
		// 0).show();
		// }
		//
		// new Thread(new Runnable() {
		// @Override
		// public void run() {
		// Log.i("test","返回的消息："+client.sendMsg("0,login,[{account:account,password:password,table_no:table_no,customer_count:customer_count,machine_code:machine_code}]"));///
		// client.closeSocket();
		// }
		// }).start();

		 Intent intent = new Intent(LodingActivity.this,LoginActivity.class);
		 startActivity(intent);
		 finish();
	}

	// 判断网络是否可用
	private void CheckNetworkConnection() {
		if (!NetworkUtil.isNetworkConnected(this)) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("没有可用的网络").setMessage("是否对网络进行设置?");
			builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = null;
					try {
						String sdkVersion = android.os.Build.VERSION.SDK;
						if (Integer.valueOf(sdkVersion) > 10) {
							intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
						} else {
							intent = new Intent();
							ComponentName comp = new ComponentName("com.android.settings",
									"com.android.settings.WirelessSettings");
							intent.setComponent(comp);
							intent.setAction("android.intent.action.VIEW");
						}
						startActivity(intent);
					} catch (Exception e) {

					}
				}
			}).setNegativeButton("否", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					finish();
				}
			}).show();
		}
	}

	class BroadcastReceiverHelper extends BroadcastReceiver {

		NotificationManager mn = null;
		Notification notification = null;
		Context ct = null;
		BroadcastReceiverHelper receiver;

		public BroadcastReceiverHelper(Context c) {
			ct = c;
			receiver = this;
		}

		// 注册
		public void registerAction(String action) {
			IntentFilter filter = new IntentFilter();
			filter.addAction(action);
			ct.registerReceiver(receiver, filter);
		}

		@Override
		public void onReceive(Context context, Intent intent) {
			String msg = intent.getStringExtra("msg");
			if (intent.getAction().equals("com.liuwang.sendMsg_lodingActivity")) {
				
				MyUtil.showDialog(LodingActivity.this, "onReceive:" + msg);
			}
		}
	}

	@Override
	protected void onStart() {
		// 注册广播接收器
		rhelper = new BroadcastReceiverHelper(this);
		rhelper.registerAction("com.liuwang.sendMsg_lodingActivity");
		super.onStart();
	}

	@Override
	protected void onStop() {
		// 取消广播接收器
		unregisterReceiver(rhelper);
		super.onStop();
	}
}